import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import tool from "../../utils/tool";

const request = require("../../utils/request.js")
const app = getApp()

var EassyPageNo=1

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    eassyList:[],
    navs: [{
			name: '动态',
			src: 'https://cdn.nlark.com/yuque/0/2019/png/280373/1568102197856-assets/web-upload/0a3840c5-1699-4507-b1d8-dfbbe0683fd0.png',
			url: './addEassy/index'
		},
		{
			name: '文章',
			src: 'https://cdn.nlark.com/yuque/0/2019/png/280373/1568102202109-assets/web-upload/93b53007-e782-47a2-ac84-c9ae854ed65d.png',
			url: './addArticle/index'
		},
		// {
		// 	name: '我的动态',
		// 	src: 'https://cdn.nlark.com/yuque/0/2019/png/280373/1568102206687-assets/web-upload/6c417267-6d87-4337-be29-7299382d0251.png',
		// 	url: ''
		// },
		// {
		// 	name: '近期热门',
		// 	src: 'https://cdn.nlark.com/yuque/0/2019/png/280373/1568102210411-assets/web-upload/0dfed72c-fbe9-46d7-8ad2-15a8cab37aa9.png',
		// 	url: ''
    // }
  ],
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    // console.log(event)
    this.setData({ active: event.detail.index });
  },
  gotoAddEassy()
  {
    // if(!app.globalData.userInfo)
    // {
    //   Dialog.alert({
    //     message: '请您先登录',
    //   }).then(() => {
    //     wx.switchTab({
    //       url: '../../pages/mine/index',
    //     })
    //   });
    // }
    // else{
      wx.navigateTo({
        url: './addEassy/index',
      })
    // }
  },
  async getEassys()
  {
    let res = await request.getEassysByPage(EassyPageNo)
    // console.log(res.data.data.value.records)
    const eassyList = res.data.data.value.records
    return eassyList
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    EassyPageNo=1
    let eassyList =await this.getEassys()
    this.setData({
      eassyList
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that =this

    if(that.data.active==0)
    {
    //提示加载中
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
    tool.debounce(async ()=> {
      //结果： 延迟函数执行，不管触发多少次都只执行最后一次。

      EassyPageNo++
      let eassyList =await that.getEassys()
      if(eassyList.length!=0)
      {
        // console.log('原数组',that.data.deliverOrderToShow)
        that.setData({
          eassyList:[...that.data.eassyList,...eassyList]
        })
      }
      else
      {
        //提示没有更多了
        Toast.fail('没有更多~');
      }
    
  },1000)()
}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})